# Copyright (c) 2006 Charl P. Botha <http://cpbotha.net/>
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Script for building envedit binaries with pyInstaller.
"""

import fnmatch
import os
import shutil
import sys

# python2.3 version (more than 1MB smaller!)
INSTALLER='c:/python23/python c:/build/installer-py2.3/build.py'
# python2.4 version
#INSTALLER='c:/python24/python c:/build/installer/build.py'

def get_our_dir():
    dirname = os.path.dirname(sys.argv[0])
    if dirname and dirname != os.curdir:
        our_dir = dirname
    else:
        our_dir = os.getcwd()

    return our_dir

def main():
    our_dir = get_our_dir()

    # delete distenvedit and buildenvedit
    for nukee in ['distenvedit', 'buildenvedit']:
        path = os.path.join(our_dir, nukee)
        if os.path.exists(nukee):
            print "Nuking %s" % (nukee,)
            shutil.rmtree(nukee)

    # also delete all pyc files that are lying around
    print "Nuking PYC files... "
    def walk_func(arg, dirname, fnames):
        fnames = [os.path.join(dirname, fname) for fname in fnames if
                  fnmatch.fnmatch(fname, '*.pyc')]
        for fname in fnames:
            os.remove(fname)

    os.path.walk(os.path.abspath(os.path.join(our_dir, '..')), walk_func, None)

    # now run the installer
    cmd = '%s %s' % (INSTALLER, os.path.join(our_dir, 'envedit.spec'))
    print cmd
    os.system(cmd)

if __name__ == '__main__':
    main()
